const router=require('@koa/router')()
const jwt = require('jsonwebtoken')
const db=require('../utils/mysql')
//鉴权中间件
const auth=async (ctx,next)=>{
    try{
        ctx.username=jwt.verify(ctx.request.header.authorization,'shdy344sgjxhhgegb').username
        let result=await new Promise((resolve,reject) => {
            // 查询身份
            const mysql=`select * from user where username=${ctx.username}`
            db(mysql,(err,data)=>{
                if(err) reject(err);
                    resolve(data)
            })
        })
        ctx.personId=result[0].personId
        ctx.identity=result[0]. identity

    }catch(err){
        ctx.body={
            code:500,
            err
        }
        return;
    }
    await next();
}

const {
    getUserInfo
}=require('../controllers/user_controller')

//初始化接口
router.get('/getUserInfo',auth,getUserInfo)

//报修模块
const {repairAdd,repairCancel,repairFast,personAssign,repersonAssign,getRepairList,repairIdea,repairReport,repairAcceptance,getRepairInfo,beginRepair}=require('../controllers/repair_controller')

//提交维修单
router.post('/repairAdd',repairAdd)
//获取报修单详细信息
router.get('/getRepairInfo',getRepairInfo)
//取消报修单
router.put('/repairCancel',repairCancel)
//催单
router.post('/repairFast',repairFast)
//指派技工
router.post('/personAssign',personAssign)
//打回改派
router.put('/repersonAssign',repersonAssign)
//获取维修单列表
router.get('/getRepairList',auth,getRepairList)
//报修单评价
router.post('/repairIdea',repairIdea)
//维修结果上报
router.post('/repairReport',repairReport)
//验收
router.post('/repairAcceptance',repairAcceptance)
//开始维修
router.put('/beginRepair',beginRepair)

//维修人员管理模块
const {personAdd,personEdit,personDelete,getpersonList,getNowPersonList,getPersonInfo}=require('../controllers/person_controller')
//新增人员
router.post('/personAdd',personAdd)
//修改人员
router.put('/personEdit',personEdit)
//删除人员
router.delete('/personDelete',personDelete)
//获取人员列表
router.get('/getpersonList',getpersonList)
//获取当前维修技工列表
router.get('/getNowPersonList',getNowPersonList)
//获取人员详情
router.get('/getPersonInfo',getPersonInfo)


//工种管理模块
const {typeAdd,typeEdit,typeDelete,getTypeList}=require('../controllers/type_controller')
//新增工种
router.post('/typeAdd',typeAdd)
//修改工种
router.put('/typeEdit',typeEdit)
//删除工种
router.delete('/typeDelete',typeDelete)
//获取工种列表
router.get('/getTypeList',getTypeList)




//配件管理模块
const {partsAdd,partsEdit,partsDelete,getPartsList,getPartsInfo}=require('../controllers/parts_controller')
//新增配件
router.post('/partsAdd',partsAdd)
//修改配件
router.put('/partsEdit',partsEdit)
//删除配件
router.delete('/partsDelete',partsDelete)
//获取配件列表
router.get('/getPartsList',getPartsList)
//获取配件详情
router.get('/getPartsInfo',getPartsInfo)



//公告管理模块
const {noticeAdd,noticeEdit,noticeDelete,getNoticeList,getNoticeInfo}=require('../controllers/notice_controller')
//新增公告
router.post('/noticeAdd',noticeAdd)
//修改公告
router.put('/noticeEdit',noticeEdit)
//删除公告
router.delete('/noticeDelete',noticeDelete)
//获取公告列表
router.get('/getNoticeList',getNoticeList)
//获取公告详情
router.get('/getNoticeInfo',getNoticeInfo)



//在线客服模块
const {getNewsList,getNewsInfo}=require('../controllers/chat_controller')
//获取消息列表
router.get('/getNewsList',getNewsList)
//获取聊天详情
router.get('/getNewsInfo',getNewsInfo)


//文件上传模块
const {upLoad}=require('../controllers/upload_controller')
const Multer=require("koa-multer")
let storage=Multer.diskStorage({
    destination:function(req,file,cb){
        cb(null,"static/upload")
    },
    filename:function(req,file,cb){
        var fileFormat = (file.originalname).split(".");
        cb(null,Date.now() + "." + fileFormat[fileFormat.length - 1]);
    }
})
let upload=Multer({storage:storage})
router.post('/upload',upload.single("file"),upLoad)



module.exports=router
